package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Message;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.OrderBean;
import com.hxkcloud.patient.beans.OrderListBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;

import java.util.ArrayList;
import java.util.List;

public class PaySuccessActivity extends BaseActivity implements View.OnClickListener {
    //标题
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_complete;

    private TextView tv_num;
    private LinearLayout ll_transaction;
    private OrderListBean orderList;
    private List<OrderBean> orderBeen;

    private TextView tv_time;
    private TextView tv_order;
    private TextView tv_commodity;
    private TextView tv_fee;
    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_success);
        pref = getSharedPreferences("paynumber",MODE_PRIVATE);
        orderBeen = new ArrayList<>();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what){
            case MessageCode.NETWORKCODE_GETUSERORDER_SUCCESS:
                if(msg.obj!=null){
                    orderList = (OrderListBean) msg.obj;
                   // Log.i("-----orderList---->",orderList.toString());
                    orderBeen = orderList.getOrder();
                    Log.i("-----orderBeen---->",orderBeen.toString());
                    tv_commodity.setText(orderBeen.get(0).getCommoditys());
                    tv_fee.setText("¥"+orderBeen.get(0).getFee());
                    tv_time.setText(orderBeen.get(0).getTime());
                    tv_order.setText(orderBeen.get(0).getOrder());
                }
                break;
            case MessageCode.NETWORKCODE_GETUSERORDER_FAILURE:
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
//                    toastMsg("失败");
                }
                break;
        }
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        tv_complete = (TextView) findViewById(R.id.tv_title_func);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText("购买");

        ll_transaction = (LinearLayout) findViewById(R.id.pay_success_trans);

        tv_time = (TextView) findViewById(R.id.pay_susses_time);
        tv_order = (TextView) findViewById(R.id.pay_success_order_number);
        tv_commodity = (TextView) findViewById(R.id.pay_succes_boughjt_item);
        tv_fee = (TextView) findViewById(R.id.pay_susses_monery);

    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        ll_transaction.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        String orderpay = pref.getString("out_trade_no","");
       // Log.i("----order--->","orderpay:"+orderpay);
        YiJiaHuNetWorkUtils.GetUserOrder(orderpay,handler);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_back:
                Intent intents = new Intent(mContext,UserFragmentActivity.class);
                intents.putExtra("paySucess",2);
                intents.putExtra("isPaySuccess",true);
                startActivity(intents);
                finish();
                break;

            case R.id.pay_success_trans:
                Intent transactionIntent = new Intent(mContext,TransactionActivity.class);
                startActivity(transactionIntent);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intents = new Intent(mContext,UserFragmentActivity.class);
            intents.putExtra("paySucess",2);
            intents.putExtra("isPaySuccess",true);
            startActivity(intents);
            finish();
            return true;
        }
            return super.onKeyDown(keyCode, event);
    }
}
